import Config from '../constants/Config';
import CheckEmptyUtils from '../constants/CheckEmptyUtils';
import { findMaxMin, splitRange } from '../utils/number';
export default class DrawModel {
    constructor(y6) {
        this.moveLineCurX = -100;
        this.screenWidth = 350;
        this.curSelected = -1;
        this.lineSpaceValue = 0;
        this.lineMinValue = 0;
        this.highlightIndex = 0;
        this.canvasContext = y6;
    }
    onSelected(x6) {
        this.onSelectListener = x6;
    }
    initMoveLineCurX() {
        this.moveLineCurX = -100;
    }
    draw(u6, v6, w6) {
        if (CheckEmptyUtils.isEmptyObj(u6))
            return;
        this.canvasContext = u6;
        this.screenWidth = v6;
        this.canvasContext.clearRect(0, 0, this.canvasContext.width, this.canvasContext.height);
        this.canvasContext.save();
        this.canvasContext.translate(0, this.canvasContext.height);
        this.canvasContext.rotate(Math.PI);
        this.canvasContext.scale(-1, 1);
        this.drawBgLine();
        this.drawHeartLine();
        this.drawHeartLineShadow();
        this.drawMoveLine();
        this.drawHeartText();
        this.drawSelectStatus();
        this.drawWave();
        this.judgeSelect();
        this.canvasContext.restore();
    }
    getXAis() {
        if (this.dataSource != null) {
            return this.dataSource.xAxis;
        }
        return [];
    }
    getYAis() {
        if (this.dataSource != null) {
            return this.dataSource.yAxis;
        }
        return [];
    }
    drawBgLine() {
        let k6 = Config.MARGIN_LEFT;
        let l6 = this.screenWidth - Config.MARGIN_LEFT;
        const m6 = this.calculateYScaleValue();
        m6.forEach((o6, p6) => {
            this.canvasContext.lineWidth = 0.5;
            this.canvasContext.lineWidth = 0.5;
            this.canvasContext.strokeStyle = Config.BG_LINE_COLOR;
            this.canvasContext.beginPath();
            this.canvasContext.moveTo(k6, p6 * Config.BG_LINE_SPACE + Config.BG_LINE_OFFSET_Y + Config.RENDER_OFFSET);
            this.canvasContext.lineTo(l6, p6 * Config.BG_LINE_SPACE + Config.BG_LINE_OFFSET_Y + Config.RENDER_OFFSET);
            this.canvasContext.stroke();
            this.canvasContext.font = Config.FONT_SIZE_NORMAL;
            let q6 = this.canvasContext.measureText(o6.toString())
                .width;
            let r6 = this.canvasContext.measureText(o6.toString())
                .height;
            let s6 = (this.screenWidth - Config.MARGIN_LEFT) - q6;
            let t6 = p6 * Config.BG_LINE_SPACE + Config.BG_LINE_OFFSET_Y + Config.RENDER_OFFSET + r6 / 2;
            this.canvasContext.fillStyle = Config.FONT_COLOR_NORMAL;
            this.rotateText(this.canvasContext, o6.toString(), s6, t6);
        });
    }
    calculateYScaleValue() {
        let b6 = [];
        this.getYAis()
            .forEach((h6) => {
            h6.getDrawPoints()
                .forEach((j6) => {
                b6.push(j6.value);
            });
        });
        const c6 = findMaxMin(b6);
        const d6 = c6[1] * (1 - 0.5);
        const e6 = c6[0] * (1 + 0.5);
        const f6 = splitRange(e6, d6);
        this.lineMinValue = f6[0];
        this.lineSpaceValue = f6[1] - f6[0];
        return f6;
    }
    drawHeartBar() {
        this.getYAis()
            .forEach((n5, o5) => {
            let p5 = 4;
            let q5 = n5.getDrawPoints()
                .length;
            let r5 = (this.screenWidth - 2 * Config.MARGIN_LEFT - 2 * Config.LINE_MARGIN) / (q5 - 1);
            n5.getDrawPoints()
                .forEach((t5, u5) => {
                let v5 = r5 * u5 + Config.MARGIN_LEFT + Config.LINE_MARGIN;
                let w5 = (t5.value - this.lineMinValue) / this.lineSpaceValue * Config.BG_LINE_SPACE + Config.BG_LINE_OFFSET_Y + Config.RENDER_OFFSET;
                let x5 = Config.RENDER_OFFSET + 15;
                this.canvasContext.lineWidth = p5;
                this.canvasContext.beginPath();
                this.canvasContext.moveTo(v5, x5);
                this.canvasContext.lineTo(v5, w5);
                this.canvasContext.stroke();
                t5.setLineX(v5);
                t5.setLineEndY(w5);
                t5.setLineSpace(r5);
                if (t5.value > 0) {
                    let y5 = v5;
                    let z5 = w5;
                    let a6 = 2;
                    this.canvasContext.fillStyle = '#E42D5F';
                    this.canvasContext.beginPath();
                    this.canvasContext.arc(y5, z5, a6, 0, 2 * Math.PI);
                    this.canvasContext.fill();
                }
            });
        });
    }
    drawHeartLine() {
        this.getYAis()
            .forEach((c5, d5) => {
            let e5 = c5.getDrawPoints()
                .length;
            let f5 = (this.screenWidth - 2 * Config.MARGIN_LEFT - 2 * Config.LINE_MARGIN) / (e5 - 1);
            let g5 = 0;
            c5.getDrawPoints()
                .forEach((i5, j5) => {
                let k5 = f5 * j5 + Config.MARGIN_LEFT + Config.LINE_MARGIN;
                let l5 = (i5.value - this.lineMinValue) / this.lineSpaceValue * Config.BG_LINE_SPACE + Config.BG_LINE_OFFSET_Y + Config.RENDER_OFFSET;
                i5.setLineX(k5);
                i5.setLineEndY(l5);
                i5.setLineSpace(f5);
                if (i5.value > 0) {
                    if (this.moveLineCurX < 0 && this.highlightIndex == j5) {
                        this.setMoveLineCur(k5, 'move');
                    }
                    if (g5 == 0) {
                        this.canvasContext.beginPath();
                        this.canvasContext.moveTo(k5, l5);
                    }
                    else {
                        this.canvasContext.lineTo(k5, l5);
                    }
                    g5 += 1;
                }
            });
            this.canvasContext.strokeStyle = c5.getColor();
            this.canvasContext.lineWidth = 1;
            this.canvasContext.stroke();
        });
    }
    drawHeartLineShadow() {
        const m4 = this.getYAis()
            .length;
        this.getYAis()
            .forEach((o4, p4) => {
            const q4 = new Path2D();
            let r4 = 0;
            let s4 = 0;
            let t4 = 0;
            let u4 = 0;
            let v4 = 0;
            o4.getDrawPoints()
                .forEach((z4, a5) => {
                if (z4.value > 0) {
                    if (r4 == 0) {
                        this.canvasContext.beginPath();
                        q4.moveTo(z4.lineX, z4.lineEndY);
                        s4 = a5;
                        u4 = z4.lineEndY;
                        v4 = z4.lineEndY;
                    }
                    else {
                        q4.lineTo(z4.lineX, z4.lineEndY);
                    }
                    t4 = a5;
                    r4 += 1;
                    if (z4.lineEndY < u4) {
                        u4 = z4.lineEndY;
                    }
                    if (z4.lineEndY > v4) {
                        v4 = z4.lineEndY;
                    }
                }
            });
            const w4 = Config.BG_LINE_OFFSET_Y + Config.RENDER_OFFSET;
            let x4 = 0;
            if (m4 > 1) {
                x4 = u4 - 20;
                if (x4 < w4) {
                    x4 = w4;
                }
            }
            else {
                x4 = w4;
            }
            q4.lineTo(o4.getDrawPoints()[t4]
                .lineX, x4);
            q4.lineTo(o4.getDrawPoints()[s4]
                .lineX, x4);
            q4.closePath();
            this.canvasContext.fillStyle = o4.getColor();
        });
    }
    drawMoveLine() {
        const h4 = '#ffffff';
        const i4 = '#A27F90';
        let j4 = 25 + Config.RENDER_OFFSET;
        let k4 = 210 + Config.RENDER_OFFSET;
        const l4 = this.canvasContext.createLinearGradient(this.moveLineCurX, j4, this.moveLineCurX, k4);
        l4.addColorStop(0, h4);
        l4.addColorStop(0.5, i4);
        l4.addColorStop(1, h4);
        this.canvasContext.lineWidth = 1;
        this.canvasContext.strokeStyle = '#dcdcdc';
        this.canvasContext.beginPath();
        this.canvasContext.moveTo(this.moveLineCurX, j4);
        this.canvasContext.lineTo(this.moveLineCurX, k4);
        this.canvasContext.stroke();
    }
    drawHeartText() {
        if (!this.getXAis()[0])
            return;
        let t3 = this.canvasContext.measureText(this.getXAis()[0])
            .width;
        let u3 = (this.screenWidth - 2 * Config.MARGIN_LEFT - 2 * Config.LINE_MARGIN) / t3;
        let v3 = Math.floor(this.getXAis()
            .length / u3) * 2;
        v3 = v3 < this.dataSource.timeStep ? this.dataSource.timeStep : v3;
        this.getXAis()
            .forEach((x3, y3) => {
            this.canvasContext.font = Config.FONT_SIZE_NORMAL;
            const z3 = this.getXAis()
                .length;
            let a4 = (this.screenWidth - 2 * Config.MARGIN_LEFT - 2 * Config.LINE_MARGIN) / (z3 - 1);
            let b4 = a4 * y3 + Config.MARGIN_LEFT + Config.LINE_MARGIN;
            let c4 = this.canvasContext.measureText(x3)
                .width;
            if (this.dataSource != null && y3 % v3 == 0) {
                let d4 = b4;
                let e4 = Config.RENDER_OFFSET + 8;
                let f4 = 0;
                let g4 = Math.abs(d4 - this.moveLineCurX);
                if (g4 < Config.TEXT_Y_OFFSET) {
                    f4 = (1 - (g4 / Config.TEXT_Y_OFFSET)) * 18;
                }
                if (g4 < Config.TEXT_Y_OFFSET) {
                    this.canvasContext.fillStyle = "#4B4B4B";
                }
                else {
                    this.canvasContext.fillStyle = Config.FONT_COLOR_NORMAL;
                }
                e4 += f4;
                this.rotateText(this.canvasContext, x3, d4 - c4 / 2, e4);
            }
        });
    }
    drawSelectStatus() {
        this.getYAis()
            .forEach((k3, l3) => {
            let m3;
            k3.getDrawPoints()
                .forEach((o3, p3) => {
                let q3 = o3.lineX;
                let r3 = o3.lineEndY;
                if (q3 == this.moveLineCurX) {
                    m3 = o3;
                    let s3 = 4;
                    this.canvasContext.fillStyle = k3.getColor();
                    this.canvasContext.beginPath();
                    this.canvasContext.arc(q3, r3, s3, 0, 2 * Math.PI);
                    this.canvasContext.fill();
                }
            });
        });
    }
    drawWaveByBgImg() {
        this.canvasContext.save();
        this.canvasContext.translate(0, 60);
        this.canvasContext.rotate(Math.PI);
        this.canvasContext.scale(-1, 1);
        this.canvasContext.fillRect(0, 50, 50, 50);
        this.canvasContext.restore();
        this.canvasContext.fillStyle = Config.ARC_BG_COLOR;
        this.canvasContext.fillRect(0, 0, this.screenWidth, Config.RENDER_OFFSET);
        let g3 = 10;
        const h3 = this.moveLineCurX;
        const i3 = Config.RENDER_OFFSET;
        this.canvasContext.fillStyle = '#FFFFFF';
        this.canvasContext.beginPath();
        this.canvasContext.arc(h3, i3, g3, 0, 2 * Math.PI);
        this.canvasContext.fill();
    }
    drawWave() {
        this.canvasContext.save();
        this.canvasContext.translate(0, 60);
        this.canvasContext.rotate(Math.PI);
        this.canvasContext.scale(-1, 1);
        let r2 = Config.WAVE_LENGTH;
        const s2 = this.moveLineCurX - r2 / 2;
        const t2 = Config.RENDER_OFFSET;
        const u2 = this.moveLineCurX + r2 / 2;
        const v2 = u2 - s2;
        const w2 = 16;
        const x2 = 0;
        const y2 = w2;
        const z2 = w2 / 2;
        this.canvasContext.translate(0, 25);
        this.canvasContext.beginPath();
        this.canvasContext.moveTo(x2, y2);
        for (let d3 = 0; d3 < 360; d3++) {
            const e3 = s2 + v2 / 360 * d3;
            const f3 = Math.cos(d3 / 360 * 2 * Math.PI) * z2 + z2;
            this.canvasContext.lineTo(e3, f3);
        }
        this.canvasContext.closePath();
        this.canvasContext.fillStyle = Config.ARC_BG_COLOR;
        this.canvasContext.fill();
        this.canvasContext.restore();
        this.canvasContext.fillStyle = Config.ARC_BG_COLOR;
        this.canvasContext.fillRect(0, 0, this.screenWidth, Config.RENDER_OFFSET);
        let a3 = 10;
        const b3 = this.moveLineCurX;
        const c3 = Config.RENDER_OFFSET;
        this.canvasContext.fillStyle = Color.White;
        this.canvasContext.beginPath();
        this.canvasContext.arc(b3, c3, a3, 0, 2 * Math.PI);
        this.canvasContext.fill();
    }
    judgeSelect() {
        this.getYAis()
            .forEach((m2, n2) => {
            m2.getDrawPoints()
                .forEach((p2, q2) => {
                if (this.moveLineCurX >= p2.lineX - p2.lineSpace / 2 && this.moveLineCurX < p2.lineX + p2.lineSpace / 2) {
                    if (this.curSelected != q2 && this.onSelectListener) {
                        this.onSelectListener(q2);
                    }
                    this.curSelected = q2;
                }
            });
        });
    }
    setDataSource(k2) {
        this.dataSource = k2;
    }
    setHighlightIndex(j2) {
        this.highlightIndex = j2;
    }
    setMoveLineCur(h2, i2) {
        this.moveLineCurX = this.judgeRange(h2, i2);
    }
    judgeRange(r1, s1) {
        this.getYAis()
            .forEach((w1, x1) => {
            let y1 = w1.getDrawPoints()
                .length;
            let z1 = (this.screenWidth - 2 * Config.MARGIN_LEFT - 2 * Config.LINE_MARGIN) / (y1 - 1);
            let a2 = (this.screenWidth - 2 * Config.MARGIN_LEFT - 2 * Config.LINE_MARGIN) / Config.ATTACH_OFFSET;
            let b2 = s1 == 'move' ? Config.ATTACH_OFFSET : z1 / 2;
            if (w1.getDrawPoints()
                .length > a2) {
                b2 = z1 / 2;
            }
            w1.getDrawPoints()
                .forEach((d2, e2) => {
                let f2 = d2.lineX;
                let g2 = d2.value;
                if (s1 == 'move') {
                    if (r1 >= f2 - b2 && r1 <= f2 + b2 && g2 > 0) {
                        r1 = f2;
                    }
                }
                else {
                    if (r1 >= f2 - b2 && r1 <= f2 + b2) {
                        r1 = f2;
                    }
                }
            });
        });
        let t1 = Config.MARGIN_LEFT + Config.LINE_MARGIN;
        let u1 = this.screenWidth - Config.MARGIN_LEFT - Config.LINE_MARGIN;
        if (r1 <= t1) {
            r1 = t1;
        }
        if (r1 >= u1) {
            r1 = u1;
        }
        return r1;
    }
    rotateText(n1, o1, p1, q1) {
        n1.save();
        n1.translate(p1, q1);
        n1.rotate(Math.PI);
        n1.scale(-1, 1);
        n1.fillText(o1, 0, 0);
        n1.restore();
    }
}
